package com.app.worldcupapp.listdetalil;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.worldcupapp.R;
import com.app.worldcupapp.bean.LoginBean;
import com.app.worldcupapp.bean.TeamsDetailInfo;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TeamDetailActivity extends AppCompatActivity {
ImageButton backId;
String id;
Intent intent;
TextView detailTeamId,victoryScore,failScore,drawScore,goalView,fumble,integral,detailInfo;
ImageView detailTeamPic;
String TAG = "TeamDetailActivity";

TeamsDetailInfo.ListDTO list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_detail);
        initView();
        initData();
        last();
        getData();


    }

    private void setData() {
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Picasso.with(getApplicationContext()).load("http://192.168.27.206:8080/statics/"+list.getTeamPic()).into(detailTeamPic);
        detailTeamId.setText(String.valueOf(list.getTeamId()));
        victoryScore.setText(String.valueOf(list.getWinMatch()));
        failScore.setText(String.valueOf(list.getLooseMatch()));
        drawScore.setText(String.valueOf(list.getPingMatch()));
        goalView.setText(String.valueOf(list.getWinGoal()));
        fumble.setText(String.valueOf(list.getLoseGoal()));
        integral.setText(String.valueOf(list.getScore())+"分");
        detailInfo.setText(list.getDetail());

    }
});


    }

    private void initView() {
        backId = findViewById(R.id.backId);
        detailTeamId = findViewById(R.id.detailTeamId);
        victoryScore = findViewById(R.id.victoryScore);
        failScore = findViewById(R.id.failScore);
        drawScore = findViewById(R.id.drawScore);
        goalView = findViewById(R.id.goalView);
        fumble = findViewById(R.id.fumble);
        integral = findViewById(R.id.integral);
        detailInfo = findViewById(R.id.detailInfo);
        detailTeamPic = findViewById(R.id.detailTeamPic);

    }
    private void getData() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("id", id)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http://192.168.27.206:8080/app19/teams/findeone")
                .post(body)
                .build();

        final Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                TeamsDetailInfo teamsDetailInfo = gson.fromJson(data, TeamsDetailInfo.class);
                if (teamsDetailInfo.isSuccess()) {
                    list = teamsDetailInfo.getList();
                    setData();
                }else {
                    Log.e(TAG,"数据不存在");
                }





            }
        });

    }

    //返回之前页面
    private void last() {
        backId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }




    private void initData() {
        intent = getIntent();
        id = intent.getStringExtra("id");
    }
}